package city.spring.modules.oldman.service.impl;

import city.spring.modules.oldman.entity.OldmanLeaveApplyEntity;
import city.spring.modules.oldman.repository.OldmanLeaveApplyRepository;
import city.spring.modules.oldman.service.OldmanLeaveApplyService;
import city.spring.modules.oldman.vo.OldmanLeaveApplyEntityVO;
import city.spring.modules.reception.entity.CheckInRecordEntity;
import city.spring.modules.reception.repository.CheckInRecordRepository;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.NonNull;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.List;

/**
 * Service：退住申请
 *
 * @author pxm
 * @date 2020-09-30 09:51:23
 */
@CacheConfig(cacheNames = {OldmanLeaveApplyServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class OldmanLeaveApplyServiceImpl extends ServiceImpl<OldmanLeaveApplyRepository, OldmanLeaveApplyEntity> implements OldmanLeaveApplyService {
    private final UserService userService;
    private final OldmanLeaveApplyRepository oldmanLeaveApplyRepository;
    private final CheckInRecordRepository checkInRecordRepository;

    public OldmanLeaveApplyServiceImpl(UserService userService, OldmanLeaveApplyRepository oldmanLeaveApplyRepository, CheckInRecordRepository checkInRecordRepository) {
        this.userService = userService;
        this.oldmanLeaveApplyRepository = oldmanLeaveApplyRepository;
        this.checkInRecordRepository = checkInRecordRepository;
    }

    @Override
    public void saveOldmanLeaveApply(OldmanLeaveApplyEntity entity, @NonNull Principal user) {
        // 登记人
        UserEntity userEntity = userService.getUserByIdOrAccount(user.getName());
        entity.setRegistrant(userEntity.getId());
        // 获取 最新的入住时间
        CheckInRecordEntity checkInRecordEntity = checkInRecordRepository.getLatestRecordByOldmanId(entity.getOldman());
        entity.setCheckInTime(checkInRecordEntity.getCheckInTime());
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateOldmanLeaveApply(OldmanLeaveApplyEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        if (!updateById(entity)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteOldmanLeaveApply(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteOldmanLeaveApply(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<OldmanLeaveApplyEntityVO> listPage(Page<? extends OldmanLeaveApplyEntity> page, Wrapper<? extends OldmanLeaveApplyEntity> wrapper) {
        return oldmanLeaveApplyRepository.listPage(page, wrapper);
    }
}